import traceback
from django.http import JsonResponse
from django.views import View
from ...serializers.unitSerializer import *
from ...config.basic_config import type_conversion_dict
from ...utils.common_util.basic_util import get_model_serializer_config
from ...utils.related_util.choice_util import get_dimension_choices, get_prefix_choices
from ...config.message_config import Config_message_type, Config_message, Error_message_type
from utils.responseContentUtil import *

class UnitConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = UnitListQuerySerializer.get_query_fields()
            single_choice_fields = ["dimension"]
            single_choice_field_dict = {}
            single_choice_field_dict["dimension"] = get_dimension_choices()
            for query_field in query_fields:
                query_field["type"] = type_conversion_dict[query_field["type"]]
                if query_field["dataIndex"] in single_choice_fields:
                    query_field["singleChoice"] = True
                    query_field["choices"] = single_choice_field_dict[query_field["dataIndex"]]
            data["query_fields"] = query_fields
            table_fields = get_model_serializer_config(UnitListSerializer)
            table_extra_fields = UnitListSerializer.get_extra_fields_info()
            for extra_field in table_extra_fields:
                for field in table_fields:
                    if field["dataIndex"] == extra_field["dataIndex"]:
                        field["type"] = extra_field["type"]
                        field["title"] = extra_field["title"]
                        break
            table_fields.append(
                {"dataIndex": "operate", "title": "操作", "type": "operate"})
            hidden_fields = ["uuid"]
            sortable_fields = []
            link_fields = ["unit_name"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                if field["dataIndex"] in link_fields:
                    field["isLink"] = True
            data["table_fields"] = table_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class UnitCreateConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            create_fields = get_model_serializer_config(UnitCreateSerializer)
            single_choice_fields = ["prefix", "dimension", "root_unit"]
            single_choice_fields_dict = {}
            single_choice_fields_dict["prefix"] = get_prefix_choices()
            single_choice_fields_dict["dimension"] = get_dimension_choices()
            single_choice_fields_dict["root_unit"] = []
            for field in create_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in single_choice_fields:
                    field["singleChoice"] = True
                    field["choices"] = single_choice_fields_dict[field["dataIndex"]]
            data["create_fields"] = create_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class UnitModifyConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            modify_fields = get_model_serializer_config(
                UnitModifyInfoSerializer)
            single_choice_fields = ["prefix", "dimension", "root_unit"]
            single_choice_fields_dict = {}
            single_choice_fields_dict["prefix"] = get_prefix_choices()
            single_choice_fields_dict["dimension"] = get_dimension_choices()
            single_choice_fields_dict["root_unit"] = []
            for field in modify_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in single_choice_fields:
                    field["singleChoice"] = True
                    field["choices"] = single_choice_fields_dict[field["dataIndex"]]
            data["modify_fields"] = modify_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class UnitInfoConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            info_fields = get_model_serializer_config(UnitInfoSerializer)
            extra_fields_info = UnitInfoSerializer.get_extra_fields_info()
            for extra_field in extra_fields_info:
                for field in info_fields:
                    if field["dataIndex"] == extra_field["dataIndex"]:
                        field["type"] = extra_field["type"]
                        field["title"] = extra_field["title"]
                        break
            for field in info_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["info_fields"] = info_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)
